/**
 * Created by Jie on 2017/2/8.
 */
var async = require('async');
var Model = require('../../model/business/devices');
var Users = require('../../model/core/users');
var Config = require('../../model/business/devices_garrison_config');
var tool = require('../../util/tools');

var index = {page:'business/devices',title:''};
var form = {page:'business/_form/_devices_form',title:'设备管理'};

exports.index = function (req,res) {
    res.render(index.page, {title: index.title});
};

exports.form = function (req,res) {
    var model = new Model();
    var config = new Config();
    var d = {title: form.title};
    async.waterfall([
        function(callback) {
            var cond = {where:'',order:' id asc'};
            config.all(cond,function (err,result) {
                if(result.error == 0){
                    callback(null,result.data);
                }else{
                    callback(null,[]);
                }
            })
        },
        function(vals, callback) {
            d.garrison = vals;
            model.one(req.params.id,function (err,result) {
                if(result.error == 0){
                    d.obj = result;
                }
                callback(null,d);
            })
        }
    ], function(error, result) {
        res.render(form.page, result);
    });

};

exports.one = function (req,res) {
    var model = new Model();
    model.one(req.params.id,function (err,result) {
        res.json(result);
    })
}

exports.insert = function (req,res) {
    var model = new Model();
    var json = req.body;
    model.insert(json,function (err,result) {
        res.json(result);
    });
}

exports.edit = function (req,res) {
    var model = new Model();
    var json = req.body;
    var id = req.params.id;
    json.id = id;
    model.edit(json,function (err,result) {
        res.json(result);
    });
}

exports.del = function (req,res) {
    var model = new Model();
    req.body.id = req.params.id;
    model.del(req.body,function (err,result) {
        res.json(result);
    });
}

exports.list = function (req,res) {
    var model = new Model();
    var where = req.body;

    async.waterfall([
        function(callback) {
            model.list(where,function (err,result) {
                callback(null,result);
            });
        },
        function(vals, callback) {
            if(vals.error == 0){
                vals.cids = tool.getIds(vals.data,'createId').join(',');
                vals.uids = tool.getIds(vals.data,'updateId').join(',');
                vals.gids = tool.getIds(vals.data,'garrisonId').join(',');
            }
            callback(null,vals);
        },
        function (vals,callback) {
            if(vals.error == 0){
                var user = new Users();
                user.findByIds(vals.cids,function (err,result) {
                    if(!err){
                        var data = vals.data;
                        for(var i=0;i<data.length;i++){
                            data[i].createName = tool.getAttrValById(data[i].createId,'name',result.data);
                        }
                        vals.data = data;
                    }
                })
            }
            callback(null,vals);
        },
        function (vals,callback) {
            if(vals.error == 0){
                var user = new Users();
                user.findByIds(vals.uids,function (err,result) {
                    if(!err){
                        var data = vals.data;
                        for(var i=0;i<data.length;i++){
                            data[i].updateName = tool.getAttrValById(data[i].updateId,'name',result.data);
                        }
                        vals.data = data;
                    }
                })
            }
            callback(null,vals);
        },
        function (vals,callback) {
            if(vals.error == 0){
                var config = new Config();
                config.findByIds(vals.uids,function (err,result) {
                    if(!err){
                        var data = vals.data;
                        for(var i=0;i<data.length;i++){
                            data[i].garrisonName = tool.getAttrValById(data[i].garrisonId,'name',result.data);
                            data[i].garrisonRange = tool.getAttrValById(data[i].garrisonId,'range',result.data);
                        }
                        vals.data = data;
                    }
                })
            }
            callback(null,vals);
        }
    ], function(error, result) {
        console.log('result',result)
        res.json(result);
    });


}

exports.all = function (req,res) {
    var model = new Model();
    var cond = {where:'',order:''};
    model.all(cond,function (err,result) {
        res.json(result);
    });
}